import request from '@/config/apiConfig/request.ts'
import requestLogin from '@/config/apiConfig/requestLogin.ts'

import { showFullScreenLoading, tryHideFullScreenLoading } from './loading'
import { userInfo } from '@/store/userInfo.ts'
import router from '@/router/index.ts'
import { msgError } from '@/units/messageUnits.ts'
import { type AxiosResponse } from 'axios'
import { i18n } from '@/lang'
const { t }: any = i18n.global

/* 接口拦截 */
async function sendHttpRequest<T>(requests: Promise<AxiosResponse>, showLoading: boolean = true): Promise<T> {
  return await new Promise((resolve, reject) => {
    if (showLoading) {
      showFullScreenLoading()
    }
    requests
      .then((response: AxiosResponse) => {
        if (showLoading) {
          tryHideFullScreenLoading()
        }
        const code = parseInt(response.data.code)
        if (code !== 200) {
          // 没有授权
          if (code === 403) {
            const userInfoStore = userInfo()
            void userInfoStore.userLogout().then(() => {
              void router.replace('/login')
            })
          } else {
            msgError(response.data.msg || t(`LG.baseModel.requestInterfaceFailed`))
          }
          reject(response) // 返回错误
        } else {
          resolve(response.data) // 返回成功
        }
      })
      .catch(({ response }): void => {
        msgError(response.data.msg || t(`LG.baseModel.requestDataException`))
        if (showLoading) {
          tryHideFullScreenLoading()
        }
        reject(response.data.msg) // 返回错误
      })
  })
}

/* 基础平台 */
// post
export async function post<T>(data: any[], showLoading?: boolean): Promise<T> {
  return await sendHttpRequest(
    request({
      url: data[0],
      method: 'post',
      data: data[1] || '',
    }),
    showLoading
  )
}
// get
export async function get<T>(data: any[], showLoading?: boolean): Promise<T> {
  return await sendHttpRequest(
    request({
      url: data[0],
      method: 'get',
      params: data[1] || '',
    }),
    showLoading
  )
}

/* 用户登录 */
// post
export async function postLogin<T>(data: any[], showLoading?: boolean): Promise<T> {
  return await sendHttpRequest(
    requestLogin({
      url: data[0],
      method: 'post',
      data: data[1] || '',
    }),
    showLoading
  )
}
// get
export async function getLogin<T>(data: any[], showLoading?: boolean): Promise<T> {
  return await sendHttpRequest(
    requestLogin({
      url: data[0],
      method: 'get',
      params: data[1] || '',
    }),
    showLoading
  )
}

// todo 这里下面后面再改
// export async function postParam(data: string[], showLoading: boolean) {
//   return await sendHttpRequest(
//     request({
//       url: data[0],
//       method: 'post',
//       params: data[1] || '',
//       data: data[1] || '',
//     }),
//     showLoading
//   )
// }
//
// export async function postFormData(url: string, data: any, showLoading: boolean) {
//   const headers = {
//     'Content-Type': 'multipart/form-data',
//   }
//   return await sendHttpRequest(
//     request({
//       url,
//       method: 'post',
//       data,
//       headers,
//     }),
//     showLoading
//   )
// }
//
// export async function getFormData(url: string, param: any, showLoading: boolean) {
//   const headers = {
//     'Content-Type': 'multipart/form-data',
//   }
//   return await sendHttpRequest(
//     request({
//       url,
//       method: 'get',
//       params: param || '',
//       headers,
//     }),
//     showLoading
//   )
// }
